package com.thoughtworks.wechatmoments.base;

import android.app.Application;

import com.thoughtworks.commonlibrary.compat.retrofit.HttpLog;
import com.thoughtworks.commonlibrary.compat.retrofit.RetrofitHelper;
import com.thoughtworks.wechatmoments.BuildConfig;
import com.thoughtworks.wechatmoments.Config;
import com.thoughtworks.wechatmoments.network.Api;

import okhttp3.logging.HttpLoggingInterceptor;

/**
 * desc   : Application的基类
 * author : guowenlong
 * Email  : guowenlong20000@gmail.com
 */
public class BaseApplication extends Application {
    private static BaseApplication mApplication = null;
    private static Api mApi = null;

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
    }

    public static BaseApplication getInstance() {
        if (mApplication == null) {
            synchronized (BaseApplication.class) {
                if (mApplication == null) {
                    mApplication = new BaseApplication();
                }
            }
        }
        return mApplication;
    }

    public static Api getApi() {
        if (mApi == null) {
            synchronized (BaseApplication.class) {
                if (mApi == null) {
                    mApi = new RetrofitHelper.Builder()
                            .setBaseUrl(Config.baseUrl)
                            .setRelease(BuildConfig.DEBUG)
                            .setLogLevel(RetrofitHelper.LogLevel.BODY)
                            .addInterceptor(new HttpLoggingInterceptor(new HttpLog()).setLevel(HttpLoggingInterceptor.Level.BODY))
                            .build()
                            .create(Api.class);
                }
            }
        }
        return mApi;
    }
}